﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.SystemManager;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Application.SystemManager
{
    public class TechnologyApp
    {
        private readonly TechnologyRepository _service = new TechnologyRepository();
        private readonly ProductRepository _serviceProduct = new ProductRepository();
        private readonly OrderRepository _serviceOrder = new OrderRepository();
        private readonly OriginalCostRepository _serviceOriginalCost = new OriginalCostRepository();
        private readonly RelationRepository _serviceRelation = new RelationRepository();
        private readonly TechnologyManageRepository _serviceTechnologyManage = new TechnologyManageRepository();
        private readonly TechnologyItemRepository _serviceTechnologyItem = new TechnologyItemRepository();
        private readonly EquipmentRepository _serviceEquipment = new EquipmentRepository();
        private readonly TechTypeRepository _serviceTechType = new TechTypeRepository();
        private readonly GradeMaintenanceRepository _serviceGradeMaintenance = new GradeMaintenanceRepository();

        public IEnumerable<ProductTechnologyView> GetTechnologyManageList(Guid productId, string productName, string productNum)
        {
            return _serviceTechnologyManage.GetList(productId, productName, productNum).OrderByDescending(a => a.CreateTime);
        }

        public TechnologyManageView GetTechnologyManageSingle(Guid productId, Guid tmId)
        {
            return _serviceTechnologyManage.GetSingle(productId, tmId);
        }
        public TechnologyManageView GetTechnologyManageSingle(Guid productId)
        {
            return _serviceTechnologyManage.GetSingle(productId);
        }

        public TechnologyManage GetTechnologyManageSingle2(Guid productId, Guid materialId)
        {
            return _serviceTechnologyManage.FindEntity(a => a.ProductId == productId && a.MaterialId == materialId);
        }
        /// <summary>
        /// 当前产品id对应的原料id集合
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public List<Guid> ContainsTmId(Guid productId)
        {
            return _serviceTechnologyManage.IQueryable().Where(a => a.ProductId == productId).Select(a => a.MaterialId).ToList();
        }

        public int InsertTechnologyManage(TechnologyManage web)
        {
            return _serviceTechnologyManage.Insert(web);
        }

        public int UpdateTechnologyManage(TechnologyManage web)
        {
            return _serviceTechnologyManage.Update(web);
        }

        public int AddOrUpdateTechnologyItem(TechnologyItem web)
        {
            var obj =
                _serviceTechnologyItem.FindEntity(
                    a => a.MaterialId == web.MaterialId && a.TechnologyId == web.TechnologyId);
            if (obj != null)
            {
                web.Id = obj.Id;
                return _serviceTechnologyItem.Update(web);
            }
            else
            {
                return _serviceTechnologyItem.Insert(web);
            }
        }

        public TechnologyItem GetSingleTechnologyItem(Guid tId, Guid mId)
        {
            return _serviceTechnologyItem.FindEntity(a => a.TechnologyId == tId && a.MaterialId == mId);
        }

        public List<ProductTechnologyView> GetTechnologyItems(Guid tId)
        {
            //return _serviceTechnologyItem.IQueryable(a => a.TechnologyId == tId).OrderByDescending(a => a.CreateTime).ToList();
            return _serviceTechnologyItem.GetTechnologyItems(tId);
        }

        /// <summary>
        /// 一个产品对应多少个原材料
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public int GetCountTechnologyManage(Guid pId)
        {
            return _serviceTechnologyManage.IQueryable().Where(a => a.ProductId == pId).ToList().Count();
        }
        /// <summary>
        /// 根据工艺tId查询出有几个原材料的数据
        /// </summary>
        /// <param name="mId"></param>
        /// <param name="tId"></param>
        /// <returns></returns>
        public int GetCountTechnologyManageItem(Guid tId)
        {
            return _serviceTechnologyItem.IQueryable().Where(a => a.TechnologyId == tId).ToList().Count();
        }

        /// <summary>
        /// 判断该工艺对应的原材料是否审核过
        /// </summary>
        /// <param name="tId"></param>
        /// <param name="mId"></param>
        /// <returns></returns>
        public int IsCheckMateria(Guid tId, Guid mId)
        {
            var obj = _serviceTechnologyItem.FindEntity(a => a.TechnologyId == tId && a.MaterialId == mId);
            if (obj != null)
            {
                return obj.Status;
            }
            return -1;
        }
        public ProductTechnologyView GetRelationProductList(Guid purchaseId, Guid productId)
        {
            return _service.GetRelationProductList(purchaseId, productId);
        }

        /*public ProductTechnologyView GetRelationProductListByPurchaseId(Guid purchaseId, Guid productId)
        {
            return _service.GetRelationProductList(orderId, productId);
        }*/

        public IEnumerable<ProductTechnologyView> GetList(int status, string productName, string productNum, string technologyNum, DateTime beginTime, DateTime endTime)
        {
            return _service.GetList(status, productName, productNum, technologyNum, beginTime, endTime).OrderByDescending(a => a.ApplyTime);
            //return _service.IQueryable().Where(a => a.Status == status || status == -10).AsQueryable().OrderByDescending(a => a.CreateTime);
        }

        public IEnumerable<ProductTechnologyView> GetListItem(string productName, string productNum, Guid techId)
        {
            return _service.GetListItem(productName, productNum, techId).OrderByDescending(a => a.ApplyTime);
        }

        public ProductTechnologyView GetFirstItem(Guid techId)
        {
            return _service.GetFirstItem(techId);
        }

        public IEnumerable<Product> GetListMaterials(int type)
        {
            return _serviceProduct.IQueryable().AsQueryable().OrderByDescending(a => a.CreateTime);
        }

        public Technology GetSingleTechnology(Guid id)
        {
            return _service.FindEntity(a => a.Id == id);
        }
        

        public Product GetSingleProduct(Guid id)
        {
            return _serviceProduct.FindEntity(a => a.Id == id);
        }

        public Technology GetSingleTechnologyById(Guid orderPruductId)
        {
            return _service.FindEntity(a => a.OrderProductId == orderPruductId);
        }

        public int AddOrUpdate(Technology web)
        {
            if (web.Id == Guid.Empty)
            {
                web.Id = Guid.NewGuid();
                return _service.Insert(web);
            }
            else
            {
                return _service.Update(web);
            }
        }



        public int UpdateProductStatus(Product web)
        {
            return _serviceProduct.Update(web);
        }

        public int UpdateOrderStatus(Order web)
        {
            return _serviceOrder.Update(web);
        }

        public int InsertOriginalCost(OriginalCost web)
        {
            return _serviceOriginalCost.Insert(web);
        }

        public int DeleteRelation(Guid firstId, Guid secondId, string typeId)
        {
            return _serviceRelation.Delete(a => a.FirstId == firstId && a.SecondId == secondId && a.TypeId == typeId);
        }

        public int InsertRelation(Relation web)
        {
            return _serviceRelation.Insert(web);
        }

        public int UpdateRelation(Relation web)
        {
            return _serviceRelation.Update(web);
        }

        public Relation GetSigleRelation(Guid firstId, Guid secondId, string typeId)
        {
            return _serviceRelation.FindEntity(a => a.FirstId == firstId && a.SecondId == secondId && a.TypeId == typeId);
        }

        public List<Relation> GetListRelation(Guid firstId, string typeId)
        {
            return _serviceRelation.IQueryable(a => a.TypeId == typeId && a.FirstId == firstId).ToList();
        }

        public IEnumerable<Equipment> GetListEquipment(string equipmentName)
        {
            return
                _serviceEquipment.IQueryable()
                    .Where(a => a.EquipmentName.Contains(equipmentName))
                    .AsQueryable()
                    .OrderBy(a => a.Id);
        }
        public List<GradeMaintenance> GetListGradeMaintenance(int type)
        {
            return _serviceGradeMaintenance.IQueryable().Where(a => a.Type == type).OrderBy(a => a.GmId).ToList();
        }

        public Equipment GetSingleEquipment(Guid id)
        {
            return _serviceEquipment.FindEntity(a => a.Id == id);
        }

        public int InsertEquipment(Equipment web)
        {
            return _serviceEquipment.Insert(web);
        }
        public int UpdateEquipment(Equipment web)
        {
            return _serviceEquipment.Update(web);
        }

        public List<TechType> GetListTechType(Guid equipmentId, string techTypeName)
        {
            return
                _serviceTechType.IQueryable()
                    .Where(a => a.TechTypeName.Contains(techTypeName) && a.EquipmentId == equipmentId)
                    .AsQueryable()
                    .OrderBy(a => a.Id).ToList();
        }

        public TechType GetSingleTechType(Guid equipmentId, int techType)
        {
            return _serviceTechType.FindEntity(a => a.EquipmentId == equipmentId && a.TechTypeId == techType);
        }
        public int InsertTechType(TechType web)
        {
            return _serviceTechType.Insert(web);
        }
        public int InsertTechTypeBatch(List<TechType> web)
        {
            return _serviceTechType.Insert(web);
        }
        public int UpdateTechType(TechType web)
        {
            return _serviceTechType.Update(web);
        }
        public int InsertGradeMaintenanceBatch(List<GradeMaintenance> web)
        {
            return _serviceGradeMaintenance.Insert(web);
        }
        public GradeMaintenance GetSingleGradeMaintenance(Guid id)
        {
            return _serviceGradeMaintenance.FindEntity(a => a.Id == id);
        }

        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <param name="gmId"></param>
        /// <param name="type">1:检验  2：加工</param>
        /// <returns></returns>
        public GradeMaintenance GetSingleGradeMaintenance(int gmId, int type)
        {
            return _serviceGradeMaintenance.FindEntity(a => a.GmId == gmId && a.Type == type);
        }
        public int UpdateGradeMaintenance(GradeMaintenance web)
        {
            return _serviceGradeMaintenance.Update(web);
        }
        public int InsertGradeMaintenance(GradeMaintenance web)
        {
            return _serviceGradeMaintenance.Insert(web);
        }
    }
}
